package com.hww.gannan.controller;

import com.hww.gannan.common.UserExport;
import com.hww.gannan.entity.FriendlyLink;
import com.hww.gannan.service.impl.AdminServiceImpl;
import com.hww.gannan.util.Result;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Date;

@RestController
@RequestMapping("/admin")
public class AdminController {

    @Resource
    private AdminServiceImpl adminService;

    @GetMapping("/getMessageList")
    public Result getMessageList(@RequestParam Integer pageNum, @RequestParam Integer pageSize) {
        return adminService.getMessageList(pageNum, pageSize);
    }

    @GetMapping("/getSysLoginList")
    public Result getSysLoginList(@RequestParam Integer pageNum, @RequestParam Integer pageSize) {
        return adminService.getSysLoginList(pageNum, pageSize);
    }

    @GetMapping("/getUrlList")
    public Result getUrlList(@RequestParam Integer pageNum, @RequestParam Integer pageSize) {
        return adminService.getUrlList(pageNum, pageSize);
    }

    @PostMapping("/addFriendlyLink")
    public Result addFriendlyLink(@RequestBody FriendlyLink friendlyLink) {
        if (!friendlyLink.getLink().startsWith("http")) {
            friendlyLink.setLink("http://" + friendlyLink.getLink());
        }

        friendlyLink.setCreateTime(new Date());
        return adminService.addFriendlyLink(friendlyLink);
    }

    @GetMapping("/delFriendlyLink")
    public Result delFriendlyLink(@RequestParam Integer id) {
        return adminService.delFriendlyLink(id);
    }

    @GetMapping("/export")
    public void export(HttpServletResponse response) throws IOException {
        adminService.export(response);
    }


}
